package com.imooc;

import com.alibaba.csp.sentinel.slots.block.RuleConstant;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRule;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager;
import org.springframework.boot.WebApplicationType;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.context.annotation.Bean;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description
 * @Author 顾志杰
 * @Date 2021/9/28 08:49
 */
@SpringBootApplication
public class SentinelApplication {

    public static void main(String[] args) {
        new SpringApplicationBuilder(SentinelApplication.class)
                .web(WebApplicationType.SERVLET)
                .run(args);
    }

    @Bean
    private static void intFlowRoles(){
        List<FlowRule> rules = new ArrayList<>();
        FlowRule rule = new FlowRule();
        /*注意 规则一定要绑定到对应的资源上 通过资源名进行绑定*/
        rule.setResource("com.imooc.controller:hello");
        rule.setGrade(RuleConstant.FLOW_GRADE_QPS);
        rule.setCount(5);
        rules.add(rule);
        /*规则管理器*/
        FlowRuleManager.loadRules(rules);
    }
}
